package com.cqc.basictransition01;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Scene;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

/**
 * getSceneForLayout()+TransitionManager.go(scene3)+new Scene()需要API>=19
 * scene1 scene2 scene3跟布局的id必须是相同的。
 * new Scene()无效果
 * <p>
 * 创建scene有2种方法
 * 1   new Scene(scene_root, container)
 * 2   Scene.getSceneForLayout(scene_root, R.layout.scene1, context)
 * <p>
 * 启动transition有2种方法
 * 1 TransitionManager.go(scene2)
 * 2 TransitionManager transitionManager = new TransitionManager();
 * transitionManager.transitionTo(scene3);
 *
 *
 * 自定义TransitionManager
 * 不使用scene的情况下怎么实现东动画效果。
 */
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioGroup;
    private FrameLayout scene_root;
    private Context context = this;
    private Scene scene1;
    private Scene scene2;
    private Scene scene3;
    private Scene scene4;
    private RelativeLayout container;
    private TransitionManager transitionManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(this);

        scene_root = (FrameLayout) findViewById(R.id.scene_root);
        container = (RelativeLayout) scene_root.findViewById(R.id.container);//也可以转换成ViewGroup


//        scene的3种创建方式 API>=19
        scene1 = Scene.getSceneForLayout(scene_root, R.layout.scene1, context);
        scene2 = Scene.getSceneForLayout(scene_root, R.layout.scene2, context);
        scene3 = Scene.getSceneForLayout(scene_root, R.layout.scene3, context);
        scene4 = Scene.getSceneForLayout(scene_root, R.layout.scene4, context);
//        scene1 = new Scene(scene_root, container);
//        scene2 = new Scene(scene_root, container);
//        scene3 = new Scene(scene_root, container);

        TransitionInflater transitionInflater = TransitionInflater.from(context);
        transitionManager = transitionInflater.inflateTransitionManager(R.transition.custom_transition_manager, scene_root);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.tb1:
                TransitionManager.go(scene1);
                break;
            case R.id.tb2:
                TransitionManager.go(scene2);
//                TransitionManager transitionManager = new TransitionManager();
//                transitionManager.transitionTo(scene3);
                break;
            case R.id.tb3:
//                TransitionManager.go(scene3);
                transitionManager.transitionTo(scene3);
                break;
            case R.id.tb4:
                transitionManager.beginDelayedTransition(scene_root);

                View view2 = findViewById(R.id.view2);
                ViewGroup.LayoutParams params = view2.getLayoutParams();
                int dimension = (int) getResources().getDimension(R.dimen.dimen50);
                params.width = dimension;
                params.height = dimension;
                view2.requestLayout();
                break;
        }
    }
}
